﻿namespace Lilium.LParse

type Recogniser<'symbol> = //TODO witness не испольузется. Убрать?
    abstract member Recognize<'result> : 
        (State<'symbol> -> Step<'result>)
        -> State<'symbol>
        -> Step<'result>

module Recogniser =
    
    open Lilium.LParse.Prelude

    let success a = 
        { new Recogniser<'symbol> with 
            member x.Recognize cont state = cont state }

    let fail = 
        { new Recogniser<'symbol> with 
            member x.Recognize cont state = Step.fail }

    let satisfy p = 
        { new Recogniser<'symbol> with 
            member x.Recognize cont state = 
                match state.Split p with
                | Some (symbol, newState) -> Step.step (cont newState)
                | None -> Step.fail }

    let compose (p : Recogniser<'symbol>) (q : Recogniser<'symbol>) =
        { new Recogniser<'symbol> with 
            member x.Recognize cont state = 
                p.Recognize (q.Recognize cont) state }
                
    let choose (p : Recogniser<'symbol>) (q : Recogniser<'symbol>) =
        { new Recogniser<'symbol> with 
            member x.Recognize cont state = 
                let a = p.Recognize cont state
                let b = q.Recognize cont state
                undefined () }